﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StoragePeriod.Data;
using System.Reflection;
using System.Linq.Expressions;
using StoragePeriod.Common;

namespace StoragePeriod.Web.SysManage
{
    public partial class Addprivilege : SecurityPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                BindData();
        }

        protected void gvprivilegeView_PreRender(object sender, EventArgs e)
        {
            //if (gvprivilegeView.Rows.Count > 0)
            //{
            //    // 使用<TH>替换<TD>
            //    gvprivilegeView.UseAccessibleHeader = true;

            //    //This will add the <thead> and <tbody> elements
            //    //HeaderRow将被<thead>包裹，数据行将被<tbody>包裹
            //    gvprivilegeView.HeaderRow.TableSection = TableRowSection.TableHeader;

            //    // FooterRow将被<tfoot>包裹
            //    //gvprivilegeView.FooterRow.TableSection = TableRowSection.TableFooter;
            //}
        }

        private void BindData()
        {

            var res = from a in Privileges.All()
                      select new
                      {
                          权限码 = a.PrivCode,
                          权限名 = a.PrivName,
                          权限描述 = a.PrivDesc
                      };
            gvprivilegeView.DataSource = res;
            gvprivilegeView.DataBind();

        }

        protected void btPost_Click(object sender, EventArgs e)
        {
            try
            {
                var newPriv = new Privileges();
                newPriv.PrivCode = tbPrivCode.Text;
                newPriv.PrivName = tbPrivName.Text;
                newPriv.PrivDesc = tbPrivDesc.Text;

                LinqUtility.Insert(newPriv);

                BindData();
            }
            catch (Exception ex)
            {
                Page.Alert("不能有重复权限码");
            }
        }

        protected void gvprivilegeView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            LinqUtility.Delete(typeof(Privileges), "PrivCode == @0", e.Keys[0].EToString());
            Page.Alert("删除成功");

            BindData();

            /*
            using (StoragePeriodDataContext dataContext = new StoragePeriodDataContext())
            {
                var rowPriv = dataContext.Privileges.FirstOrDefault(p => p.PrivCode == e.Keys[0].EToString());

                UpdateContextData("Update", rowPriv);

                //dataContext.Privileges.DeleteOnSubmit(rowPriv);
                //dataContext.SubmitChanges();
                Page.Alert("删除成功");
            }
            BindData();*/
        }

        protected void gvprivilegeView_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvprivilegeView.EditIndex = e.NewEditIndex;
            BindData();
        }

        protected void gvprivilegeView_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                var updatePriv = new
                {
                    PrivName = e.NewValues[0],
                    PrivDesc = e.NewValues[1]
                };
                Privileges.Update(updatePriv, "PrivCode == @0", e.Keys[0].EToString());
                gvprivilegeView.EditIndex = -1;

                BindData();
                Page.Alert("更新成功");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void gvprivilegeView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvprivilegeView.EditIndex = -1;

            BindData();
        }

        protected void gvRoleprivilegeView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            (sender as GridView).PageIndex = e.NewPageIndex;
            BindData();
        }
    }
}